﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class categoryedit : BasePage
    {
        WTODataContext contex = new WTODataContext();
        int cid = 0;
        /// <summary>
        /// 更新模式
        /// </summary>
        protected bool UpdateMode
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    cid = int.Parse(Request.QueryString["id"]);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BuildTheParentCategory();
                if (UpdateMode)
                {
                    GetInfo();
                }
            }
        }

        private void GetInfo()
        {
            category cat = contex.categories.SingleOrDefault(p => p.art_kind_id == cid);
            if (cat != null)
            {
                this.ddlParentCategory.Items.FindByValue(cat.art_parent_kind.ToString()).Selected = true;
                this.ddlParentCategory.Enabled = false;
                this.txtCategoryName.Text = cat.art_kind_name;
                this.txtAlias.Text = cat.alias;
                this.txtOrder.Text = cat.sort_num.ToString();
                this.chkVisiable.Checked = cat.visiable.Value;
                this.chkSingle.Checked = cat.single_page.HasValue ? cat.single_page.Value : false;
                this.ddlPosition.Items.FindByValue(cat.position.ToString()).Selected = true;
                this.txtURL.Text = cat.url;
                if (cat.is_special_to_child.HasValue)
                {
                    this.chkSpecial.Checked = cat.is_special_to_child.Value == 'Y' ? true : false;
                }
                else
                {
                    this.chkSpecial.Checked = false;
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('参数传递失败');", true);
            }
        }


        private void BuildTheParentCategory()
        {
            this.ddlParentCategory.Items.Clear();
            //Dictionary<string, string> dic = Utility.GetXML(Server.MapPath("..\\metadate\\Navigation.xml"));
            //foreach(var ite in dic)
            //{
            //    ListItem item = new ListItem();
            //    item.Value = ite.Key;
            //    item.Text = ite.Value;
            //    this.ddlParentCategory.Items.Add(item);
            //}
            var parent = contex.categories.Where(p => p.art_parent_kind == 0).ToList();
            foreach (category cat in parent)
            {
                this.ddlParentCategory.Items.Add(new ListItem(cat.art_kind_name, cat.art_kind_id.ToString()));
            }
            this.ddlParentCategory.Items.Insert(0, new ListItem("--设置为一级分类--", "0"));
        }

       

        protected void btnOK_Click(object sender, EventArgs e)
        {
            category cat;
            int order_num = 0;
            if (UpdateMode)
            {
                cat = contex.categories.SingleOrDefault(p => p.art_kind_id == cid);
                if (cat == null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('参数传递失败');", true);
                    return;
                }
            }
            else
                cat = new category();
            cat.art_parent_kind = Utility.ConvertIntFromString(this.ddlParentCategory.SelectedItem.Value);
            cat.art_kind_name = this.txtCategoryName.Text.Trim();
            cat.update_date = DateTime.Now;
            cat.alias = this.txtAlias.Text.Trim();
            int.TryParse(this.txtOrder.Text.Trim(), out order_num);
            cat.sort_num = order_num;
            cat.visiable = this.chkVisiable.Checked;
            cat.position = this.ddlPosition.SelectedValue;
            cat.single_page = this.chkSingle.Checked;
            cat.url = this.txtURL.Text.Trim();
            cat.is_special_to_child = this.chkSpecial.Checked ? 'Y' : 'N';
            if (!UpdateMode)
            {
                cat.created_date = DateTime.Now;
                contex.categories.InsertOnSubmit(cat);
            }
            contex.SubmitChanges();
            Response.Redirect("categoryList.aspx");
        }

      
    }
}